// =================================================================
// File: tree.cpp
// Desc: Contains function for printing out contents of the file
//       system from the current directory in a tree format.
// =================================================================


// =================================================================
// INCLUDES
// =================================================================
#include <iostream>
#include <string>
#include <map>
#include "FSManager.h"
#include "Directory.h"
#include "commands.h"
#include "Inode.h"

using namespace std;
// =================================================================
// FUNCTIONS
// =================================================================

// =================================================================
// Name: tree
// Desc: Prints the file system from the current directory in a tree
//       format.
// Args: FSManager* fsm - pointer to active FSManager object
// =================================================================

void tree(FSManager *fsm) 
{
  static int indent = 0;
  
  Directory *dir = fsm->GetCurrentDirectory();

  if (dir == NULL) {
    cerr << "Current directory is NULL\n";
    return;
  }

  map<string, int> entries = dir->GetEntries();
  map<string, int>::iterator it;

  for(it = entries.begin(); it != entries.end(); it++)
  {
    string filename = it->first;
    if (filename == "./" || filename == "../")
      continue;
    for (int i = 0; i < indent; i++)
      cout << "-";
    Inode *node = fsm->ReadInode(it->second);
    cout << it->first <<  " Size:" << node->GetSize() << endl;
    delete node;
    if (filename[filename.length()-1] == '/')
    {
      indent++;
      Directory* new_dir = fsm->ReadDirectory(filename);
      fsm->SetCurrentDirectory(new_dir);
      tree(fsm);
    }
  }
  if (indent > 0)
  {
    indent--;
    Directory* cur_dir = fsm->ReadDirectory("../");
    fsm->SetCurrentDirectory(cur_dir);
  }
}
